package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.pojo.Bed;
import com.zzyl.nursing.domain.pojo.Floor;
import com.zzyl.nursing.domain.pojo.Room;
import com.zzyl.nursing.domain.vo.FloorVo;
import com.zzyl.nursing.domain.vo.TreeVo;
import com.zzyl.nursing.mapper.BedMapper;
import com.zzyl.nursing.mapper.FloorMapper;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 楼层Service业务层处理
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor> implements IFloorService {
    @Autowired
    private FloorMapper floorMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BedMapper bedMapper;
    
    /**
     * 查询楼层
     * @param id 楼层主键
     * @return 楼层
     */
    @Override
    public Floor selectFloorById(Long id) {
        return getById(id);
    }
    
    /**
     * 新增楼层
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int insertFloor(Floor floor) {
        return save(floor) ? 1 : 0;
    }
    
    /**
     * 修改楼层
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int updateFloor(Floor floor) {
        return updateById(floor) ? 1 : 0;
    }
    
    /**
     * 批量删除楼层
     * @param ids 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }
    
    /**
     * 删除楼层信息
     * @param id 楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorById(Long id) {
        return removeById(id) ? 1 : 0;
    }
    
    /**
     * 查询所有楼层（负责老人）
     */
    @Override
    public List<Floor> selectAllByNur() {
        return floorMapper.selectAllByNur();
    }
    
    @Override
    public List<TreeVo> getRoomAndBedByBedStatus(Integer status) {
        // 1-查询所有楼List<Floor>、所有房List<Room>、所有床List<Bed>
        // 1.1 所有楼
        List<Floor> floorList=this.list();
        // 1.2 所有房
        Set<Long> floorIdSet=floorList.stream()
                                      .map(Floor::getId)
                                      .collect(Collectors.toSet());
        List<Room> roomList=roomMapper.selectList(Wrappers.<Room>lambdaQuery()
                                                          .in(Room::getFloorId,floorIdSet));
        
        // 1.3 所有床
        Set<Long> roomIdSet=roomList.stream()
                                    .map(Room::getId)
                                    .collect(Collectors.toSet());
        List<Bed> bedList=bedMapper.selectList(Wrappers.<Bed>lambdaQuery()
                                                       .in(Bed::getRoomId,roomIdSet)
                                                       .eq(Bed::getBedStatus,status));
        // 2-构建Map<Long roomId,List<TreeVo> bedList> bedMap：List<Bed>按照roomId分组
        Map<Long, List<TreeVo>> bedMap=bedList.stream()
                                              .collect(Collectors.groupingBy(Bed::getRoomId,Collectors.mapping(bed -> TreeVo.builder()
                                                                                                                            .value(String.valueOf(bed.getId()))
                                                                                                                            .label(bed.getBedNumber())
                                                                                                                            .build(),Collectors.toList())));
        // 3-构建Map<Long floorId,List<TreeVo> roomList> roomMap: List<Room>按照floorId进行分组，将每一个Room转换TreeVo(value+label+children)-->bedMap.get(roomId)
        Map<Long, List<TreeVo>> roomMap=roomList.stream()
                                                .collect(Collectors.groupingBy(Room::getFloorId,Collectors.mapping(room -> TreeVo.builder()
                                                                                                                                 .value(String.valueOf(room.getId()))
                                                                                                                                 .label(room.getCode())
                                                                                                                                 .children(bedMap.getOrDefault(room.getId(),Collections.emptyList()))
                                                                                                                                 .build(),Collectors.toList())));
        // 4-List<Floor>循环遍历：Floor->TreeVo(value+label+children)--->roomMap.get(floorId)
        return floorList.stream()
                        .map(floor -> TreeVo.builder()
                                            .value(String.valueOf(floor.getId()))
                                            .label(floor.getName())
                                            .children(roomMap.getOrDefault(floor.getId(),Collections.emptyList()))
                                            .build())
                        .collect(Collectors.toList());
    }
    
    @Override
    public List<FloorVo> getAllFloorsWithDevice() {
        return this.getBaseMapper()
                   .getAllFloorsWithDevice();
    }
    
}